﻿
using QLKS2014.BLL;
using QLKS2014.PUBLIC;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace QLKS2014.GUI.StupidContent
{
    /// <summary>
    /// tìm kiếm phòng theo tên
    /// </summary>
    public partial class SearchingByRoomType : UserControl, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string _PropertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(_PropertyName));
        }
        ObservableCollection<LoaiPhongPublic> _ocLoaiPhong;

        public ObservableCollection<LoaiPhongPublic> OcLoaiPhong
        {
            get { return _ocLoaiPhong; }
            set
            {
                _ocLoaiPhong = value;
                OnPropertyChanged("OcLoaiPhong");
            }
        }

        ObservableCollection<PhongPublic> _ocPhong;

        public ObservableCollection<PhongPublic> OcPhong
        {
            get { return _ocPhong; }
            set
            {
                _ocPhong = value;
                OnPropertyChanged("OcPhong");
            }
        }

        public SearchingByRoomType()
        {
            InitializeComponent();
            Loaded += SearchByRoom_Loaded;
        }

        /// <summary>
        /// load ten cac loai phong man hinh tim kiem
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void SearchByRoom_Loaded(object sender, RoutedEventArgs e)
        {
            _initialize();
        }

        private void _initialize()
        {
            // init ocloaiphong
            List<LoaiPhongPublic> lLoaiPhong = new LoaiPhongBll().selectAll();
            if (lLoaiPhong != null)
                OcLoaiPhong = new ObservableCollection<LoaiPhongPublic>(lLoaiPhong);
            else
                OcLoaiPhong = new ObservableCollection<LoaiPhongPublic>();

            // init empty OcPhong 
            OcPhong = new ObservableCollection<PhongPublic>();

            // init selected index of combobox
            if(cbbRoomType.HasItems)
                cbbRoomType.SelectedIndex = 0;
        }

        /// <summary>
        /// xu ly su kien tim kiem phong theo loai phong
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            _searchRoom();
        }

        private void _searchRoom()
        {
            if (cbbRoomType.Text != "")
            {
                PhongPublic phong0 = new PhongPublic();
                phong0.MaLoaiPhong = int.Parse(cbbRoomType.SelectedValue.ToString());
                List<PhongPublic> lPhong = new PhongBll().selectByMaLoaiPhong(phong0);
                if (lPhong != null)
                    OcPhong = new ObservableCollection<PhongPublic>(lPhong);
            }
            else
            {
                MessageBox.Show("Mời chọn loại phòng!");
            }
        }

        private void SearchByRoomType_KeyDown(object sender, KeyEventArgs e)
        {
            
        }

        private void dgRoom_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                _searchRoom();
            }
        }

        private void dgRoom_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = (e.Row.GetIndex() + 1).ToString();
        }
    }
}
